﻿/*************************************************************************
 *  Copyright (C), 2016-2017, Mogoson tech. Co., Ltd.
 *  FileName: MCEditor.cs
 *  Author: Mogoson   Version: 1.0   Date: 9/1/2016
 *  Version Description:
 *    Internal develop version,mainly to achieve its function.
 *  File Description:
 *    Ignore.
 *  Class List:
 *    <ID>           <name>             <description>
 *     1.           MCEditor               Ignore.
 *  Function List:
 *    <class ID>     <name>             <description>
 *     1.
 *  History:
 *    <ID>    <author>      <time>      <version>      <description>
 *     1.     Mogoson     9/1/2016       1.0        Build this file.
 *************************************************************************/

namespace Developer.MCTool
{
    using System.Collections.Generic;
    using UnityEditor;
    using UnityEngine;

    /// <summary>
    /// Mesh Combine Editor.
    /// </summary>
    public class MCEditor : ScriptableWizard
    {
        #region Property and Field
        [Tooltip("Root of meshe gameObject.")]
        public GameObject meshesRoot;

        [Tooltip("Target to save combine meshes.")]
        public GameObject meshSave;
        #endregion

        #region Private Method
        [MenuItem("Tool/MCEditor &M")]
        static void ShowMCEditor()
        {
            DisplayWizard("MCEditor", typeof(MCEditor), "Combine");
        }//Show...()_end

        //Window update.
        void OnWizardUpdate()
        {
            if (meshesRoot && meshSave)
                isValid = true;
            else
                isValid = false;
        }//OnW...()_end

        //Combine button click.
        void OnWizardCreate()
        {
            //Combine meshes that in meshesRoot's children to a new mesh.
            var meshFilters = meshesRoot.GetComponentsInChildren<MeshFilter>();
            var combines = new CombineInstance[meshFilters.Length];
            var materialList = new List<Material>();
            for (int i = 0; i < meshFilters.Length; i++)
            {
                combines[i].mesh = meshFilters[i].sharedMesh;
                combines[i].transform = Matrix4x4.TRS(meshFilters[i].transform.position - meshesRoot.transform.position,
                    meshFilters[i].transform.rotation, meshFilters[i].transform.lossyScale);
                var materials = meshFilters[i].GetComponent<MeshRenderer>().sharedMaterials;
                foreach (var material in materials)
                {
                    materialList.Add(material);
                }//foreach()_end
            }//for()_end
            var newMesh = new Mesh();
            newMesh.CombineMeshes(combines, false);
            ;

            //Add the new mesh to the meshSave.
            meshSave.AddComponent<MeshFilter>().sharedMesh = newMesh;
            meshSave.AddComponent<MeshCollider>().sharedMesh = newMesh;
            meshSave.AddComponent<MeshRenderer>().sharedMaterials = materialList.ToArray();
        }//OnW...()_end
        #endregion
    }//class_end
}//namespace_end